{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8a0b585c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:42:58.907901Z",
     "start_time": "2022-11-18T13:42:51.825848Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导包\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "82307abd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T12:53:30.514220Z",
     "start_time": "2022-11-16T12:53:30.505244Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.0.0'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6b63c485",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T12:54:33.964456Z",
     "start_time": "2022-11-16T12:54:33.921569Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看GPU是否可用\n",
    "torch.cuda.is_available()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9f3e6a17",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T12:54:49.622560Z",
     "start_time": "2022-11-16T12:54:49.609595Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'NVIDIA GeForce RTX 3050 Ti Laptop GPU'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看GPU设备\n",
    "torch.cuda.get_device_name()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86d2ac4e",
   "metadata": {},
   "source": [
    "### pytorch张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "515c6993",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T12:56:06.584642Z",
     "start_time": "2022-11-16T12:56:06.569682Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 2., 3., 4.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tensor 可以在GPU上加速运算. \n",
    "# 创建一个tensor\n",
    "torch.tensor([1, 2, 3, 4], dtype=torch.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ab2c0930",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T12:56:47.746512Z",
     "start_time": "2022-11-16T12:56:36.402861Z"
    }
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "63e40255",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T12:56:54.631090Z",
     "start_time": "2022-11-16T12:56:54.449576Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'tensorflow' has no attribute 'tensor'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_6868\\1776056503.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# tensorflow 中没有tensor方法来直接创建tensor, tensorflow中创建tensor都是通过constant, Variable\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# pytorch中没有常量变量的说法, 统一都是变量.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mtf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtensor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: module 'tensorflow' has no attribute 'tensor'"
     ]
    }
   ],
   "source": [
    "# tensorflow 中没有tensor方法来直接创建tensor, tensorflow中创建tensor都是通过constant, Variable\n",
    "# pytorch中没有常量变量的说法, 统一都是变量. \n",
    "tf.tensor([1, 2, 3, 4], dtype=tf.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "707ada4b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T12:59:53.919390Z",
     "start_time": "2022-11-16T12:59:53.912409Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "815f472c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T12:59:56.291047Z",
     "start_time": "2022-11-16T12:59:56.209263Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 2., 3., 4.])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以通过ndarray来创建tensor\n",
    "torch.tensor(np.array([1, 2, 3, 4]), dtype=torch.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e7696387",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:00:23.749577Z",
     "start_time": "2022-11-16T13:00:23.737609Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2, 3, 4], dtype=torch.int32)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor(np.array([1, 2, 3, 4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "18f29d0c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:14:02.602910Z",
     "start_time": "2022-11-16T13:14:02.583961Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy 中有一些routines函数(常规函数), 可以让我们快速的创建ndarray.\n",
    "# torch中也有这样的函数, 连名字都差不多, 功能是一样的. \n",
    "np.ones((3, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "81bc8407",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:14:23.216756Z",
     "start_time": "2022-11-16T13:14:23.142954Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.ones(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0fcfd776",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:16:01.310805Z",
     "start_time": "2022-11-16T13:16:01.302826Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.ones(*(3, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2e9eb940",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:16:23.839534Z",
     "start_time": "2022-11-16T13:16:23.823569Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((3, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "27a92853",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:16:32.209136Z",
     "start_time": "2022-11-16T13:16:32.191181Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "010691c4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:16:50.093284Z",
     "start_time": "2022-11-16T13:16:50.087299Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 1., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 1., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "086c76f1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:17:03.302940Z",
     "start_time": "2022-11-16T13:17:03.271025Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 0., 0., 0., 0., 0., 0., 0.],\n",
       "        [0., 1., 0., 0., 0., 0., 0., 0.],\n",
       "        [0., 0., 1., 0., 0., 0., 0., 0.],\n",
       "        [0., 0., 0., 1., 0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0., 1., 0., 0., 0.],\n",
       "        [0., 0., 0., 0., 0., 1., 0., 0.],\n",
       "        [0., 0., 0., 0., 0., 0., 1., 0.],\n",
       "        [0., 0., 0., 0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.eye(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "28373924",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:17:26.076008Z",
     "start_time": "2022-11-16T13:17:26.015171Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 8, 8, 8],\n",
       "       [8, 8, 8, 8],\n",
       "       [8, 8, 8, 8]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((3, 4), fill_value=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0d95dea0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:18:05.398798Z",
     "start_time": "2022-11-16T13:18:05.374861Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[8, 8, 8, 8],\n",
       "        [8, 8, 8, 8],\n",
       "        [8, 8, 8, 8]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.full((3, 4), 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "946e1a80",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:18:25.197823Z",
     "start_time": "2022-11-16T13:18:25.183860Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0.        ,   2.04081633,   4.08163265,   6.12244898,\n",
       "         8.16326531,  10.20408163,  12.24489796,  14.28571429,\n",
       "        16.32653061,  18.36734694,  20.40816327,  22.44897959,\n",
       "        24.48979592,  26.53061224,  28.57142857,  30.6122449 ,\n",
       "        32.65306122,  34.69387755,  36.73469388,  38.7755102 ,\n",
       "        40.81632653,  42.85714286,  44.89795918,  46.93877551,\n",
       "        48.97959184,  51.02040816,  53.06122449,  55.10204082,\n",
       "        57.14285714,  59.18367347,  61.2244898 ,  63.26530612,\n",
       "        65.30612245,  67.34693878,  69.3877551 ,  71.42857143,\n",
       "        73.46938776,  75.51020408,  77.55102041,  79.59183673,\n",
       "        81.63265306,  83.67346939,  85.71428571,  87.75510204,\n",
       "        89.79591837,  91.83673469,  93.87755102,  95.91836735,\n",
       "        97.95918367, 100.        ])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0, 100, )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a5c7ccff",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:19:17.121897Z",
     "start_time": "2022-11-16T13:19:17.111923Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([  0.0000,   2.0408,   4.0816,   6.1224,   8.1633,  10.2041,  12.2449,\n",
       "         14.2857,  16.3265,  18.3673,  20.4082,  22.4490,  24.4898,  26.5306,\n",
       "         28.5714,  30.6122,  32.6531,  34.6939,  36.7347,  38.7755,  40.8163,\n",
       "         42.8571,  44.8980,  46.9388,  48.9796,  51.0204,  53.0612,  55.1020,\n",
       "         57.1429,  59.1837,  61.2245,  63.2653,  65.3061,  67.3469,  69.3878,\n",
       "         71.4286,  73.4694,  75.5102,  77.5510,  79.5918,  81.6327,  83.6735,\n",
       "         85.7143,  87.7551,  89.7959,  91.8367,  93.8775,  95.9184,  97.9592,\n",
       "        100.0000])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.linspace(0, 100, 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "06a21d97",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:20:19.868014Z",
     "start_time": "2022-11-16T13:20:19.847070Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7.57078942,  7.62640896, 13.26774293,  2.86481841, 11.83263521,\n",
       "        12.34075489],\n",
       "       [ 8.49821798,  9.21910608,  6.3984334 , 13.62876322, 16.46467508,\n",
       "        11.8140126 ],\n",
       "       [ 9.26063068,  7.17044959,  8.10019884, 12.4162967 , 14.91681618,\n",
       "         8.39253031],\n",
       "       [13.48390163, 13.04434526,  7.32414995,  0.02470767, 10.64433855,\n",
       "         7.99346332],\n",
       "       [13.43322513, 13.08431756, 11.39862531, 12.09430618,  3.85741263,\n",
       "        14.5198482 ]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.normal(loc=10, scale=3, size=(5, 6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b0e9f03a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:22:49.239875Z",
     "start_time": "2022-11-16T13:22:49.200979Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1.5337, 0.6013, 4.0573, 3.2413, 4.4263, 5.7397, 6.8983, 8.5341, 9.0422,\n",
       "        9.9219])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch中随机数不需加random\n",
    "torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7ac42f9a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:23:54.161173Z",
     "start_time": "2022-11-16T13:23:54.137238Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.2856,  0.1230,  0.7389,  1.2756,  0.6001],\n",
       "        [-1.0147, -0.3550, -1.2774, -0.9994,  1.8197],\n",
       "        [-0.3327, -1.1594, -0.0996,  0.4943,  0.1348],\n",
       "        [-0.5838, -0.5642,  0.7396,  0.8806, -0.2443]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准正态分布\n",
    "torch.randn(4, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d5a259b6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:24:24.649600Z",
     "start_time": "2022-11-16T13:24:24.581780Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.0410, 0.2836, 0.7346],\n",
       "        [0.8655, 0.8262, 0.1303]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0到1之间的随机数\n",
    "torch.rand(2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6aff70f2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:25:34.654299Z",
     "start_time": "2022-11-16T13:25:34.638338Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[5, 8, 0, 8, 6],\n",
       "        [7, 0, 0, 2, 6],\n",
       "        [1, 8, 1, 1, 6],\n",
       "        [2, 7, 7, 6, 6]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = torch.randint(0, 10, size=(4, 5))\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "30c17771",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:25:51.066384Z",
     "start_time": "2022-11-16T13:25:51.047435Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 5, 8, 8, 2],\n",
       "       [6, 6, 9, 5, 3],\n",
       "       [5, 0, 3, 8, 3],\n",
       "       [3, 2, 1, 9, 1]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.random.randint(0, 10, size=(4, 5))\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "6dd9b591",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:28:12.576270Z",
     "start_time": "2022-11-16T13:28:12.565298Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ndarray 的基础属性: ndim(维度), shape(形状), size(元素总个数), dtype(元素类型)\n",
    "# tensor基本一样, 但是没有总个数, 多了个size()方法\n",
    "t.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "8e268260",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:29:58.392150Z",
     "start_time": "2022-11-16T13:29:58.373201Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 5])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b24b98b6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:28:40.933397Z",
     "start_time": "2022-11-16T13:28:40.919435Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 5])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# size()和.shape返回的东西是一样的. 都是形状. \n",
    "t.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "6e59b54e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:28:48.507135Z",
     "start_time": "2022-11-16T13:28:48.502147Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.int64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "6e9e177d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:29:37.356434Z",
     "start_time": "2022-11-16T13:29:37.341474Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.size(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4478b782",
   "metadata": {},
   "source": [
    "### tensor的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5b5cab6f",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (113207785.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\ASUS\\AppData\\Local\\Temp\\ipykernel_6868\\113207785.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    torch.float32 32位浮点数\u001b[0m\n\u001b[1;37m                  ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "torch.float32 32位浮点数\n",
    "torch.float64\n",
    "toch.int32\n",
    "torch.int16\n",
    "torch.int64\n",
    "torch.long 这个经常在pytorch中用来做target的数据类型. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "45198100",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:34:22.150950Z",
     "start_time": "2022-11-16T13:34:22.141974Z"
    }
   },
   "outputs": [],
   "source": [
    "# 你可以在创建tensor的时候, 指定类型, 使用dtype即可. \n",
    "t1 = torch.tensor([1, 2, 3, 4], dtype=torch.float32)\n",
    "t2 = torch.tensor([1, 2, 3, 4], dtype=torch.long)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "af48c755",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:34:26.947118Z",
     "start_time": "2022-11-16T13:34:26.936147Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 2., 3., 4.])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "2b68e40d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:34:30.651207Z",
     "start_time": "2022-11-16T13:34:30.645224Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2, 3, 4])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "cccffaee",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:34:40.842938Z",
     "start_time": "2022-11-16T13:34:40.837951Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.long"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9f8c7f31",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:35:15.737195Z",
     "start_time": "2022-11-16T13:35:15.709266Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2., 4., 6., 8.])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 + t2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b84a195",
   "metadata": {},
   "source": [
    "### tensor运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "db061909",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:36:38.929603Z",
     "start_time": "2022-11-16T13:36:38.917635Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2., 3., 4., 5.])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tensor和单个数字运算\n",
    "# tensor中的每个元素分别和这个数字运算\n",
    "t1 + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "48146e19",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:37:05.668063Z",
     "start_time": "2022-11-16T13:37:05.653102Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2., 4., 6., 8.])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tensor 和tensor运算.\n",
    "# 要求形状相同, 对应位置的元素进行计算\n",
    "t1 + t2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "54c2a4cc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:39:17.663898Z",
     "start_time": "2022-11-16T13:39:17.652927Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0],\n",
       "       [1],\n",
       "       [2],\n",
       "       [3]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果形状不同, pytorch中有没有广播机制.\n",
    "# 1. 维度不够补维度. \n",
    "# 2. 数据不够, 补数据, 只会用已有的唯一的数据进行补充. \n",
    "a = np.arange(4).reshape(4, 1)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "b08a244b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:39:27.506563Z",
     "start_time": "2022-11-16T13:39:27.501576Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.arange(4)\n",
    "b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "8ccaefee",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:41:31.163708Z",
     "start_time": "2022-11-16T13:41:31.156727Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3],\n",
       "       [1, 2, 3, 4],\n",
       "       [2, 3, 4, 5],\n",
       "       [3, 4, 5, 6]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "5c07b52c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:47:07.556663Z",
     "start_time": "2022-11-16T13:47:07.543697Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3],\n",
       "       [0, 1, 2, 3]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([[0, 1, 2, 3], [0, 1, 2, 3]])\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "6bdf17c0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:47:42.008484Z",
     "start_time": "2022-11-16T13:47:41.987539Z"
    }
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (4,1) (2,4) ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_53180\\2107169428.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,1) (2,4) "
     ]
    }
   ],
   "source": [
    "a + c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "eb2742d1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:49:29.928243Z",
     "start_time": "2022-11-16T13:49:29.916275Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 4, 6],\n",
       "       [0, 2, 4, 6]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b + c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "4406889c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:50:05.804254Z",
     "start_time": "2022-11-16T13:50:05.795278Z"
    }
   },
   "outputs": [],
   "source": [
    "ta = torch.tensor(a)\n",
    "tb = torch.tensor(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "3e110169",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:50:11.586783Z",
     "start_time": "2022-11-16T13:50:11.573817Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[0],\n",
       "         [1],\n",
       "         [2],\n",
       "         [3]], dtype=torch.int32),\n",
       " tensor([0, 1, 2, 3], dtype=torch.int32))"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ta, tb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "72ead412",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:50:17.780212Z",
     "start_time": "2022-11-16T13:50:17.766249Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1, 2, 3],\n",
       "        [1, 2, 3, 4],\n",
       "        [2, 3, 4, 5],\n",
       "        [3, 4, 5, 6]], dtype=torch.int32)"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 说明torch中有和numpy一样的广播机制. \n",
    "ta + tb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "058a5999",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:51:20.850462Z",
     "start_time": "2022-11-16T13:51:18.483795Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(4, 4), dtype=int32, numpy=\n",
       "array([[0, 1, 2, 3],\n",
       "       [1, 2, 3, 4],\n",
       "       [2, 3, 4, 5],\n",
       "       [3, 4, 5, 6]])>"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tensorflow中有没有广播机制? 也有\n",
    "t_a = tf.constant(a)\n",
    "t_b = tf.constant(b)\n",
    "t_a + t_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "c3728cea",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:53:33.925431Z",
     "start_time": "2022-11-16T13:53:33.907478Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[   nan, 0.0000, 0.0000, 0.0000],\n",
       "        [   inf, 1.0000, 0.5000, 0.3333],\n",
       "        [   inf, 2.0000, 1.0000, 0.6667],\n",
       "        [   inf, 3.0000, 1.5000, 1.0000]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.add()\n",
    "ta.add(tb) \n",
    "ta.subtract(tb)\n",
    "# element-wise乘法. 按位乘即: 对应位置的元素相乘. \n",
    "ta.multiply(tb)\n",
    "ta.divide(tb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "112317a5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:55:40.634411Z",
     "start_time": "2022-11-16T13:55:40.619454Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([14])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(1, 4) @ b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "8af57056",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:56:04.063724Z",
     "start_time": "2022-11-16T13:56:04.050759Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([14], dtype=torch.int32)"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵的点乘 要求第一个矩阵的列数等于第二个矩阵的行数 ..\n",
    "ta.reshape(1, 4) @ tb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "ac767e5c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:56:42.649486Z",
     "start_time": "2022-11-16T13:56:42.636520Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([14], dtype=torch.int32)"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# matrix multiply\n",
    "ta.reshape(1, 4).matmul(tb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "193b6f90",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:57:47.349376Z",
     "start_time": "2022-11-16T13:57:47.331424Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1],\n",
       "        [2],\n",
       "        [3],\n",
       "        [4]], dtype=torch.int32)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pytorch有专门可以修改原始数据的方法. 都是以下划线结尾的方法. \n",
    "ta.add(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "c2aa7c62",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:58:03.896103Z",
     "start_time": "2022-11-16T13:58:03.883138Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1],\n",
       "        [2],\n",
       "        [3],\n",
       "        [4]], dtype=torch.int32)"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pytorch中所有加了下划线结尾的函数, 都是会直接修改原始数据的. \n",
    "ta.add_(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "f78e9e07",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-16T13:58:14.098313Z",
     "start_time": "2022-11-16T13:58:14.083353Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1],\n",
       "        [2],\n",
       "        [3],\n",
       "        [4]], dtype=torch.int32)"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19128922",
   "metadata": {},
   "outputs": [],
   "source": [
    "# tensor的索引, 切片, 变形. 和numpy完全一样. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "003a6c14",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:17:09.744383Z",
     "start_time": "2022-11-18T12:17:09.729423Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 3, 2, 6, 6],\n",
       "        [8, 0, 7, 3, 7],\n",
       "        [4, 6, 5, 8, 6],\n",
       "        [7, 7, 4, 9, 8]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tensor的索引: 和numpy完全一样. \n",
    "t = torch.randint(0, 10, size=(4, 5))\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "7897d620",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:17:18.452086Z",
     "start_time": "2022-11-18T12:17:18.433135Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0)"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0, 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "c7d14931",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:17:34.944957Z",
     "start_time": "2022-11-18T12:17:34.929000Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0, 3, 2, 6, 6])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "4bfd6f4f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:17:56.298823Z",
     "start_time": "2022-11-18T12:17:56.282866Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[8, 0, 7, 3, 7],\n",
       "        [4, 6, 5, 8, 6]])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 左闭右开\n",
    "t[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "b0af6240",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:18:44.899295Z",
     "start_time": "2022-11-18T12:18:44.875359Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[3, 2],\n",
       "        [0, 7],\n",
       "        [6, 5],\n",
       "        [7, 4]])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[:, 1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "fec56e0b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:18:56.688752Z",
     "start_time": "2022-11-18T12:18:56.677781Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 7],\n",
       "        [6, 5]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[1:3, 1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "33b5233e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:19:16.572551Z",
     "start_time": "2022-11-18T12:19:16.553602Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 5])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "28523b97",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:19:52.170309Z",
     "start_time": "2022-11-18T12:19:52.161330Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0],\n",
       "         [3],\n",
       "         [2],\n",
       "         [6],\n",
       "         [6]],\n",
       "\n",
       "        [[8],\n",
       "         [0],\n",
       "         [7],\n",
       "         [3],\n",
       "         [7]],\n",
       "\n",
       "        [[4],\n",
       "         [6],\n",
       "         [5],\n",
       "         [8],\n",
       "         [6]],\n",
       "\n",
       "        [[7],\n",
       "         [7],\n",
       "         [4],\n",
       "         [9],\n",
       "         [8]]])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.reshape(4, 5, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "39c2e4dd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:20:34.726444Z",
     "start_time": "2022-11-18T12:20:34.704503Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0],\n",
       "         [3],\n",
       "         [2],\n",
       "         [6],\n",
       "         [6]],\n",
       "\n",
       "        [[8],\n",
       "         [0],\n",
       "         [7],\n",
       "         [3],\n",
       "         [7]],\n",
       "\n",
       "        [[4],\n",
       "         [6],\n",
       "         [5],\n",
       "         [8],\n",
       "         [6]],\n",
       "\n",
       "        [[7],\n",
       "         [7],\n",
       "         [4],\n",
       "         [9],\n",
       "         [8]]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最后一个维度, None表示占个位置, \n",
    "t[:, :, None]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "3b526311",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:21:09.041631Z",
     "start_time": "2022-11-18T12:21:09.028666Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0],\n",
       "         [3],\n",
       "         [2],\n",
       "         [6],\n",
       "         [6]],\n",
       "\n",
       "        [[8],\n",
       "         [0],\n",
       "         [7],\n",
       "         [3],\n",
       "         [7]],\n",
       "\n",
       "        [[4],\n",
       "         [6],\n",
       "         [5],\n",
       "         [8],\n",
       "         [6]],\n",
       "\n",
       "        [[7],\n",
       "         [7],\n",
       "         [4],\n",
       "         [9],\n",
       "         [8]]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ...表示前面所有的维度. \n",
    "t[..., None]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "2bc50579",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:23:37.064288Z",
     "start_time": "2022-11-18T12:23:37.057307Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 5])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 中间的维度也能挤. \n",
    "t[:, None, :].squeeze().shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "1057a274",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:23:06.957840Z",
     "start_time": "2022-11-18T12:23:06.952854Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 5])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把没有数据的维度挤压掉. \n",
    "t.reshape(4, 5, 1).squeeze().shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "a098ead1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:25:23.821651Z",
     "start_time": "2022-11-18T12:25:23.816664Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 1, 5])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# unsqueeze跟squeeze相反. \n",
    "t.unsqueeze(dim=1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65531317",
   "metadata": {},
   "outputs": [],
   "source": [
    "t.expand()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "8cb136f1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:27:33.697165Z",
     "start_time": "2022-11-18T12:27:33.680204Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 1])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([[1], [2], [3]])\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "5849af54",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:28:00.854497Z",
     "start_time": "2022-11-18T12:28:00.800641Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "The expanded size of the tensor (5) must match the existing size (3) at non-singleton dimension 0.  Target sizes: [5, 4].  Tensor sizes: [3, 1]",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_53180\\1565617941.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexpand\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m: The expanded size of the tensor (5) must match the existing size (3) at non-singleton dimension 0.  Target sizes: [5, 4].  Tensor sizes: [3, 1]"
     ]
    }
   ],
   "source": [
    "# 跟广播机制一样, 只能用唯一的数据去扩充. \n",
    "x.expand(5, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "4f913ec5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:30:07.869658Z",
     "start_time": "2022-11-18T12:30:07.815802Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(32, 224, 224, 3)"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# batch_size, W, H, C\n",
    "n = np.random.random((32, 224, 224, 3))\n",
    "n.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "47955ddf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:31:09.650359Z",
     "start_time": "2022-11-18T12:31:09.632407Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(224, 224)"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n[0, :, :, 0].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "15ea76a1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:31:38.336606Z",
     "start_time": "2022-11-18T12:31:38.304692Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([32, 224, 224, 3])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = torch.tensor(n)\n",
    "t.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "cb197c3a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:31:45.861473Z",
     "start_time": "2022-11-18T12:31:45.849505Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([224, 224])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0, :, :, 0].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8bddc64",
   "metadata": {},
   "outputs": [],
   "source": [
    "# tensor变形也是用reshape\n",
    "# view 也是变形的意思. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "98ee5a73",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:32:41.966360Z",
     "start_time": "2022-11-18T12:32:41.946414Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4, 7, 5, 6, 3],\n",
       "        [7, 6, 7, 3, 7],\n",
       "        [4, 2, 4, 6, 5],\n",
       "        [8, 9, 5, 2, 6]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = torch.randint(0, 10, size=(4, 5))\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "162df72e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:32:49.628859Z",
     "start_time": "2022-11-18T12:32:49.617888Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4, 7, 5, 6],\n",
       "        [3, 7, 6, 7],\n",
       "        [3, 7, 4, 2],\n",
       "        [4, 6, 5, 8],\n",
       "        [9, 5, 2, 6]])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.reshape(5, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "28ec591a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:33:10.197824Z",
     "start_time": "2022-11-18T12:33:10.175883Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4, 7, 5, 6],\n",
       "        [3, 7, 6, 7],\n",
       "        [3, 7, 4, 2],\n",
       "        [4, 6, 5, 8],\n",
       "        [9, 5, 2, 6]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.view(5, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b560f910",
   "metadata": {},
   "outputs": [],
   "source": [
    "view和reshape是一样的吗?\n",
    "# view对连续存储的数据进行reshape\n",
    "# reshape没限制. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83d76681",
   "metadata": {},
   "outputs": [],
   "source": [
    "网上那种numpy 100题拿来练有用吗? \n",
    "非常有用, 赶紧去练. \n",
    "# numpy pandas matplotlib, seaborn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "71d7327e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:37:54.947474Z",
     "start_time": "2022-11-18T12:37:54.935506Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(106)"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 聚合运算\n",
    "# pytorch聚合的时候不指定维度和numpy表现是一样的, 会把所有的维度聚合成一个数字. \n",
    "t.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "745d8338",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:41:04.982526Z",
     "start_time": "2022-11-18T12:41:04.966569Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 1])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pytorch中维度不叫axis, dim dimension\n",
    "# keepdim保持被聚合掉的维度继续存在. \n",
    "t.sum(dim=1, keepdim=True).shape # 4, 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f839d9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# max min, mean, median, sum, argmin, argmax, std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "87b6a7e2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:43:23.673448Z",
     "start_time": "2022-11-18T12:43:23.662477Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4, 7, 5, 6, 3],\n",
       "        [7, 6, 7, 3, 7],\n",
       "        [4, 2, 4, 6, 5],\n",
       "        [8, 9, 5, 2, 6]])"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "ffe40b38",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:43:18.840379Z",
     "start_time": "2022-11-18T12:43:18.814448Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 0, 3, 1])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.argmax(dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "c1002761",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:44:57.128908Z",
     "start_time": "2022-11-18T12:44:57.119932Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 4, 1, 3, 6],\n",
       "        [4, 0, 0, 1, 9],\n",
       "        [6, 5, 8, 3, 3],\n",
       "        [4, 1, 3, 3, 4]])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tensor的拼接\n",
    "t1 = torch.randint(0, 10, size=(4, 5))\n",
    "t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "f92106ad",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:45:02.589298Z",
     "start_time": "2022-11-18T12:45:02.582317Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[2, 7, 9, 7, 7],\n",
       "        [2, 4, 2, 6, 7],\n",
       "        [0, 5, 5, 1, 2],\n",
       "        [6, 5, 3, 9, 6]])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2 = torch.randint(0, 10, size=(4, 5))\n",
    "t2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "c2cb74b1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:46:50.705026Z",
     "start_time": "2022-11-18T12:46:50.695053Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(100)"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pytorch也有dot, 但是, 仅限于向量之间的运算. \n",
    "# 一维向量的点乘. \n",
    "t1[0].dot(t2[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "84b85bd6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:49:03.487756Z",
     "start_time": "2022-11-18T12:49:03.453847Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 4, 1, 3, 6],\n",
       "        [4, 0, 0, 1, 9],\n",
       "        [6, 5, 8, 3, 3],\n",
       "        [4, 1, 3, 3, 4],\n",
       "        [2, 7, 9, 7, 7],\n",
       "        [2, 4, 2, 6, 7],\n",
       "        [0, 5, 5, 1, 2],\n",
       "        [6, 5, 3, 9, 6]])"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# concatenate\n",
    "# 默认也是按照axis=0, 即dim=0\n",
    "torch.concat((t1, t2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "68276ddb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:49:31.930654Z",
     "start_time": "2022-11-18T12:49:31.912704Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 4, 1, 3, 6, 2, 7, 9, 7, 7],\n",
       "        [4, 0, 0, 1, 9, 2, 4, 2, 6, 7],\n",
       "        [6, 5, 8, 3, 3, 0, 5, 5, 1, 2],\n",
       "        [4, 1, 3, 3, 4, 6, 5, 3, 9, 6]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.concat((t1, t2), dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "cd1df20c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:50:15.837179Z",
     "start_time": "2022-11-18T12:50:15.758390Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 4, 1, 3, 6, 2, 7, 9, 7, 7],\n",
       "        [4, 0, 0, 1, 9, 2, 4, 2, 6, 7],\n",
       "        [6, 5, 8, 3, 3, 0, 5, 5, 1, 2],\n",
       "        [4, 1, 3, 3, 4, 6, 5, 3, 9, 6]])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 水平拼接\n",
    "torch.hstack((t1, t2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "b771f826",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:50:48.677314Z",
     "start_time": "2022-11-18T12:50:48.654374Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 4, 1, 3, 6],\n",
       "        [4, 0, 0, 1, 9],\n",
       "        [6, 5, 8, 3, 3],\n",
       "        [4, 1, 3, 3, 4],\n",
       "        [2, 7, 9, 7, 7],\n",
       "        [2, 4, 2, 6, 7],\n",
       "        [0, 5, 5, 1, 2],\n",
       "        [6, 5, 3, 9, 6]])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.vstack((t1, t2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "cb1c55f5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:51:08.323747Z",
     "start_time": "2022-11-18T12:51:08.317763Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4, 7, 9, 1, 6, 1],\n",
       "        [1, 0, 1, 5, 6, 6],\n",
       "        [1, 9, 4, 8, 2, 6],\n",
       "        [8, 4, 5, 1, 8, 1],\n",
       "        [8, 0, 2, 6, 9, 5]])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t3 = torch.randint(0, 10, size=(5, 6))\n",
    "t3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "37225fad",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:51:20.005492Z",
     "start_time": "2022-11-18T12:51:19.910745Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "Sizes of tensors must match except in dimension 1. Expected size 4 but got size 5 for tensor number 1 in the list.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_53180\\1957939458.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhstack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mt1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mt3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m: Sizes of tensors must match except in dimension 1. Expected size 4 but got size 5 for tensor number 1 in the list."
     ]
    }
   ],
   "source": [
    "torch.hstack((t1, t3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "dd6a2827",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:51:40.941988Z",
     "start_time": "2022-11-18T12:51:40.902093Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "Sizes of tensors must match except in dimension 0. Expected size 5 but got size 6 for tensor number 1 in the list.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_53180\\4206189431.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvstack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mt1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mt3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m: Sizes of tensors must match except in dimension 0. Expected size 5 but got size 6 for tensor number 1 in the list."
     ]
    }
   ],
   "source": [
    "torch.vstack((t1, t3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "5c33b91f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:52:37.434835Z",
     "start_time": "2022-11-18T12:52:37.429848Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 5])"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "8c83bd32",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:55:02.366569Z",
     "start_time": "2022-11-18T12:55:02.358591Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[4, 7, 5, 6, 3]]),\n",
       " tensor([[7, 6, 7, 3, 7],\n",
       "         [4, 2, 4, 6, 5]]),\n",
       " tensor([[8, 9, 5, 2, 6]]))"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.split([1, 2, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "0dbad0a8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:53:01.306963Z",
     "start_time": "2022-11-18T12:53:01.295993Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4, 7, 5, 6, 3],\n",
       "        [7, 6, 7, 3, 7],\n",
       "        [4, 2, 4, 6, 5],\n",
       "        [8, 9, 5, 2, 6]])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "f64ae25b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:55:17.730463Z",
     "start_time": "2022-11-18T12:55:17.726473Z"
    }
   },
   "outputs": [],
   "source": [
    "import glob"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "dcfc400d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:55:40.563371Z",
     "start_time": "2022-11-18T12:55:40.557387Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.\\\\pytorch入门.ipynb']"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "glob.glob('./*.ipynb')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "de7d6ee2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:56:30.035006Z",
     "start_time": "2022-11-18T12:56:30.024035Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 0, 1, 3])"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.permutation([0, 1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "b38e38fd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:58:03.024207Z",
     "start_time": "2022-11-18T12:58:03.015231Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 5])"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "2877e710",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:58:34.054183Z",
     "start_time": "2022-11-18T12:58:34.021272Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([5, 4])"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 就是转置, 就是transpose\n",
    "torch.permute(t, [1, 0]).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "2dc91ab9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T12:59:13.570959Z",
     "start_time": "2022-11-18T12:59:13.564975Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([5, 4])"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.transpose(1, 0).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "935ecb6d",
   "metadata": {},
   "source": [
    "### 张量的自动微分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d5c89917",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:43:00.666195Z",
     "start_time": "2022-11-18T13:43:00.657219Z"
    }
   },
   "outputs": [],
   "source": [
    "# 自动微分其实就是求导. pytorch帮我们求导. \n",
    "# requires_grad=True表示要跟踪x的梯度即导数\n",
    "x = torch.ones(2, 2, requires_grad=True)\n",
    "\n",
    "y = 2 * x + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "a7d2764e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:28:43.082714Z",
     "start_time": "2022-11-18T13:28:43.066757Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1.],\n",
       "        [1., 1.]], requires_grad=True)"
      ]
     },
     "execution_count": 167,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "864f6587",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:28:47.952684Z",
     "start_time": "2022-11-18T13:28:47.929745Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4., 4.],\n",
       "        [4., 4.]], grad_fn=<AddBackward0>)"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "c4461d2a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:29:42.569554Z",
     "start_time": "2022-11-18T13:29:42.161644Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "grad can be implicitly created only for scalar outputs",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_53180\\4190252357.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 求导一定要求是从标量开始.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0my\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\.venv\\lib\\site-packages\\torch\\_tensor.py\u001b[0m in \u001b[0;36mbackward\u001b[1;34m(self, gradient, retain_graph, create_graph, inputs)\u001b[0m\n\u001b[0;32m    361\u001b[0m                 \u001b[0mcreate_graph\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcreate_graph\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    362\u001b[0m                 inputs=inputs)\n\u001b[1;32m--> 363\u001b[1;33m         \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mautograd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgradient\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mretain_graph\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0minputs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    364\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    365\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mregister_hook\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhook\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\.venv\\lib\\site-packages\\torch\\autograd\\__init__.py\u001b[0m in \u001b[0;36mbackward\u001b[1;34m(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)\u001b[0m\n\u001b[0;32m    164\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    165\u001b[0m     \u001b[0mgrad_tensors_\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_tensor_or_tensors_to_tuple\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgrad_tensors\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtensors\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 166\u001b[1;33m     \u001b[0mgrad_tensors_\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_make_grads\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtensors\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgrad_tensors_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mis_grads_batched\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    167\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mretain_graph\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    168\u001b[0m         \u001b[0mretain_graph\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\.venv\\lib\\site-packages\\torch\\autograd\\__init__.py\u001b[0m in \u001b[0;36m_make_grads\u001b[1;34m(outputs, grads, is_grads_batched)\u001b[0m\n\u001b[0;32m     65\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mout\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrequires_grad\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     66\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[0mout\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnumel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 67\u001b[1;33m                     \u001b[1;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"grad can be implicitly created only for scalar outputs\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     68\u001b[0m                 \u001b[0mnew_grads\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mones_like\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mout\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmemory_format\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpreserve_format\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     69\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: grad can be implicitly created only for scalar outputs"
     ]
    }
   ],
   "source": [
    "# 求导一定要求是从标量开始. 标量就是一个数字. \n",
    "y.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "c605966a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:31:20.280120Z",
     "start_time": "2022-11-18T13:31:20.258179Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AddBackward0 at 0x29293422288>"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.grad_fn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "53cf6655",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:31:32.062595Z",
     "start_time": "2022-11-18T13:31:32.042649Z"
    }
   },
   "outputs": [],
   "source": [
    "z = y.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "57092497",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:31:41.011651Z",
     "start_time": "2022-11-18T13:31:40.993699Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(4., grad_fn=<MeanBackward0>)"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "b8247695",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:32:25.339052Z",
     "start_time": "2022-11-18T13:32:25.299156Z"
    }
   },
   "outputs": [],
   "source": [
    "z.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "id": "8334fab0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:32:48.686584Z",
     "start_time": "2022-11-18T13:32:48.675611Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.5000, 0.5000],\n",
       "        [0.5000, 0.5000]])"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# y = 2x + 2, y对x的导数是2, x有4个值, 平均, 一下, 每个值的导数就是2/ 4=0.5\n",
    "x.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "id": "0b285293",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:35:40.744227Z",
     "start_time": "2022-11-18T13:35:40.730265Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\.venv\\lib\\site-packages\\torch\\_tensor.py:1104: UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won't be populated during autograd.backward(). If you indeed want the .grad field to be populated for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. If you access the non-leaf Tensor by mistake, make sure you access the leaf Tensor instead. See github.com/pytorch/pytorch/pull/30531 for more informations. (Triggered internally at  C:\\actions-runner\\_work\\pytorch\\pytorch\\builder\\windows\\pytorch\\build\\aten\\src\\ATen/core/TensorBody.h:475.)\n",
      "  return self._grad\n"
     ]
    }
   ],
   "source": [
    "y.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "2019413f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:38:43.565581Z",
     "start_time": "2022-11-18T13:38:43.548626Z"
    }
   },
   "outputs": [],
   "source": [
    "y.retain_grad()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c6530f6f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:43:13.268476Z",
     "start_time": "2022-11-18T13:43:13.256508Z"
    }
   },
   "outputs": [],
   "source": [
    "z = y * y * 3\n",
    "out = z.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6643306b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:43:15.247183Z",
     "start_time": "2022-11-18T13:43:15.160414Z"
    }
   },
   "outputs": [],
   "source": [
    "out.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "17d3a632",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:43:43.013890Z",
     "start_time": "2022-11-18T13:43:42.992946Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[12., 12.],\n",
       "        [12., 12.]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "56fa5c89",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:44:36.592536Z",
     "start_time": "2022-11-18T13:44:36.584557Z"
    }
   },
   "outputs": [],
   "source": [
    "# pytorch的版本发生了变化, 现在这个pytorch版本获取不到非叶子节点的导数了. \n",
    "y.retain_grad()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6c53ee49",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:46:57.267659Z",
     "start_time": "2022-11-18T13:46:57.232752Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(x ** 2).requires_grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d35839d4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:47:20.465593Z",
     "start_time": "2022-11-18T13:47:20.456615Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "# 如果不需要求导. 可以torch.no_grad()上下文来进行计算. \n",
    "# 经常用在预测的时候. \n",
    "with torch.no_grad():\n",
    "    # 在with语句块中的任何计算, 都不会进行求导. \n",
    "    print((x ** 2).requires_grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a588e090",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:49:06.615578Z",
     "start_time": "2022-11-18T13:49:06.603611Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# detach\n",
    "y = x ** 2 + 2\n",
    "# 因为x可以求导, 那么y自然也可以求导. \n",
    "y.requires_grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "226fd0a2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:50:02.249725Z",
     "start_time": "2022-11-18T13:50:02.232771Z"
    }
   },
   "outputs": [],
   "source": [
    "# 返回的tensor其实就是y的值, 这个新的tensor是不能求导的, 因为detach掉了. \n",
    "# 相当于拷贝了一份y的值, 并且对这个值不求导. \n",
    "z = y.detach()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "45dc677e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:50:08.424205Z",
     "start_time": "2022-11-18T13:50:08.412237Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z.requires_grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f146f1e4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:50:24.246870Z",
     "start_time": "2022-11-18T13:50:24.232908Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.requires_grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8c17440b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:53:18.684149Z",
     "start_time": "2022-11-18T13:53:18.680160Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 默认一个tensor是不能求导. \n",
    "t = torch.randn(3, 3)\n",
    "t.requires_grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2cde061e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:54:11.301368Z",
     "start_time": "2022-11-18T13:54:11.296381Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.4985,  1.2438, -0.9422],\n",
       "        [-0.3971,  0.1725, -1.4228],\n",
       "        [-0.1768, -0.2805,  1.0932]], requires_grad=True)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以通过requires_grad_() 修改requires_grad属性\n",
    "t.requires_grad_(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6d5ad62d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-18T13:54:20.857799Z",
     "start_time": "2022-11-18T13:54:20.839847Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.requires_grad"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
